package com.example.ffepeg.ffpeg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;

public class DeleteFilesContent {
//删除文件不是非常安全

    public static void main(String[] args) {
        String path = "D:\\javaXM\\FFEpeg\\src\\main\\resources\\file\\scratchFile";
        deleteFilesContent(path);


        /*String jsonString = "[{\"name\":\"11213\"},{\"name\":\"123\"}]";
        JSONArray jsonArray = JSON.parseArray(jsonString);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String name = jsonObject.getString("name");
            System.out.println(name);
        }*/

    }

    public static boolean deleteFilesContent(String path){
        File folder = new File(path);
        if (folder.isDirectory()) {
            String[] entries = folder.list();
            if (entries != null) {
                for (String entry : entries) {
                    File currentFile = new File(folder, entry);
                    deleteRecursively(currentFile);
                }
            }
        } else {
            System.out.println("指定的路径不是目录.");
            return false;
        }
        return true;
    }

    private static void deleteRecursively(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    deleteRecursively(f);
                }
            }
        }
        // 删除文件或空目录
        if (!file.delete()) {
            System.out.println("删除失败: " + file);
        } else {
            System.out.println("删除成功: " + file);
        }
    }
}
